//
//  CWHomeCell.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/26.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import SnapKit

/**
 首页展示的cell
 */

class CWHomeCell: UITableViewCell {
    //记录toolbar顶部约束
    var toolbarTopConstranint: Constraint?
    
    var statusViewModel: CWStatusViewModel? {
        
        didSet{
            
            originalView.originalViewModel = statusViewModel
            
            //卸载之前额toolbar约束
            toolbarTopConstranint?.deactivate()
            
            //判断是否有转发微博
            if statusViewModel?.statusModel?.retweeted_status != nil {
                
                //有转发微博
                retweetView.isHidden = false
                
                toolbar.snp.makeConstraints({ (make) in
                    toolbarTopConstranint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                retweetView.retweetViewModel = statusViewModel
                
            }else {
                
                //没有转发微博
                retweetView.isHidden = true
                
                toolbar.snp.makeConstraints({ (make) in
                    toolbarTopConstranint = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
            
            
            
        }
    }
    
    
    
    //懒加载原创微博视图
    fileprivate lazy var originalView: CWHomeOriginalView = CWHomeOriginalView()
    
    //懒加载转发微博视图
    fileprivate lazy var retweetView: CWHomeRetweetView = CWHomeRetweetView()
    
    //懒加载toolbar
    fileprivate lazy var toolbar: CWHomeToolbar = CWHomeToolbar()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolbar)
        
        originalView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalTo(contentView)
            //make.bottom.equalTo(contentView).offset(-homeCellMargin)
        }
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
        }
        toolbar.snp.makeConstraints { (make) in
            make.top.equalTo(retweetView.snp.bottom)
            make.leading.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
    }

}
